package util;

import com.mysql.cj.protocol.Resultset;

import javax.xml.transform.Result;
import java.sql.*;
import java.util.ResourceBundle;

    public class JdbcUtil {


        private static String username = null;
        private static String password = null;
        private static String url = null;

        static {

            try {
                //加载配置文件
                ResourceBundle resourceBundle = ResourceBundle.getBundle("druid");
                url = resourceBundle.getString("url");
                username = resourceBundle.getString("username");
                password = resourceBundle.getString("password");
            } catch (Exception exception) {
                exception.printStackTrace();
            }


        }

        //获取连接
        public static Connection getcon() {
            Connection con = null;
            try {
                //创建驱动
                Class.forName("com.mysql.cj.jdbc.Driver");
                //获取连接
                con = DriverManager.getConnection(url, username, password);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return con;
        }

        //关闭连接
        public static void close(ResultSet rs, Statement st, Connection con) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (st != null) {
                    try {
                        st.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (con != null) {
                        try {
                            con.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
